package com.ruihu.admin.module.business.task.controller;

import com.deepoove.poi.XWPFTemplate;
import com.ruihu.admin.common.anno.NoValidPrivilege;
import com.ruihu.admin.common.anno.OperateLog;
import com.ruihu.admin.common.domain.PageResultDTO;
import com.ruihu.admin.common.controller.BaseController;
import com.ruihu.admin.common.domain.ResponseDTO;
import com.ruihu.admin.common.exception.BusinessException;
import com.ruihu.admin.module.business.task.constant.TaskResponseCodeConst;
import com.ruihu.admin.module.business.task.domain.dto.SubTaskAddDTO;
import com.ruihu.admin.module.business.task.domain.dto.SubTaskUpdateDTO;
import com.ruihu.admin.module.business.task.domain.dto.SubTaskQueryDTO;
import com.ruihu.admin.module.business.task.domain.vo.SubTaskAllVO;
import com.ruihu.admin.module.business.task.domain.vo.SubTaskVO;
import com.ruihu.admin.module.business.task.service.SubTaskService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.Workbook;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Objects;

/**
 * [ 任务子表 ]
 *
 * @author Tony
 * @version 1.0
 * @company 瑞虎（南京）信息技术有限公司
 * @copyright (c)  瑞虎（南京）信息技术有限公司Inc. All rights reserved.
 * @date 2022-08-29 17:13:25
 * @since JDK1.8
 */
@RestController
@Api(tags = {"任务子表"})
@OperateLog
public class SubTaskController extends BaseController {

    @Autowired
    private SubTaskService subTaskService;

    @ApiOperation(value = "分页查询任务子表",notes = "@author Tony")
    @PostMapping("/subTask/page/query")
    public ResponseDTO<PageResultDTO<SubTaskVO>> queryByPage(@RequestBody SubTaskQueryDTO queryDTO) {
        return subTaskService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "分页查询子任务",notes = "@author Tony")
    @PostMapping("/subtask/page/querySubTask")
    public ResponseDTO<PageResultDTO<SubTaskVO>> querySubTaskByPage(@RequestBody @Validated SubTaskQueryDTO queryDTO) {
        return this.subTaskService.querySubTaskByPage(queryDTO);
    }

    @ApiOperation(value = "添加任务子表",notes = "@author Tony")
    @PostMapping("/subTask/add")
    public ResponseDTO<String> add(@RequestBody @Validated SubTaskAddDTO addTO){
        return subTaskService.add(addTO);
    }

    @ApiOperation(value="修改任务子表",notes = "@author Tony")
    @PostMapping("/subTask/update")
    public ResponseDTO<String> update(@RequestBody @Validated SubTaskUpdateDTO updateDTO){
        return subTaskService.update(updateDTO);
    }

    @ApiOperation(value="查询子任务下详情",notes = "@author Tony")
    @GetMapping("/subTask/subTaskNodeAllInst/{subTaskId}")
    public ResponseDTO<SubTaskAllVO> querySubTaskNodeAllInst(@PathVariable("subTaskId") Long subTaskId) {
        return subTaskService.querySubTaskInstById(subTaskId);
    }

    @ApiOperation(value = "重置子任务", notes = "重置子任务")
    @PostMapping("/subTask/{subTaskId}/reset")
    @NoValidPrivilege
    public ResponseDTO<String> resetSubTask(@PathVariable("subTaskId") Long subTaskId) {
        return this.subTaskService.resetSubTask(subTaskId);
    }

    @ApiOperation(value = "撤销归档", notes = "撤销归档")
    @PostMapping("/subTask/{subTaskId}/cancelarchive")
    @NoValidPrivilege
    public ResponseDTO<String> cancelSubTaskArchive(@PathVariable("subTaskId") Long subTaskId) {
        return this.subTaskService.cancelSubTaskArchive(subTaskId);
    }

    @ApiOperation(value = "导出电芯拆解记录", notes = "导出电芯拆解记录")
    @PostMapping("/subTask/{subTaskId}/export")
    public void exportSubTaskInfo(@PathVariable("subTaskId") Long subTaskId, HttpServletResponse response) throws Exception {
        XWPFTemplate wordTemplate = this.subTaskService.exportSubTaskByTemplate(subTaskId);
        if (Objects.nonNull(wordTemplate)) {
            String fileName = "电芯失效分析报告" + DateFormatUtils.format(new Date(), "yyyyMMdd");
            super.downloadWord(fileName, wordTemplate, response);
        } else {
            throw new BusinessException(TaskResponseCodeConst.EXPORT_ERROR);
        }
    }
}
